package logic

import (
	"context"
	"fmt"
	"product_zc/models/mysql"

	"product_zc/productRpc/internal/svc"
	"product_zc/productRpc/productRpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateProductLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateProductLogic {
	return &UpdateProductLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateProductLogic) UpdateProduct(in *productRpc.UpdateProductRequest) (*productRpc.UpdateProductResponse, error) {
	// todo: add your logic here and delete this line

	product := mysql.Product{}
	mysql.DB.Where("user_id=? and id=?", in.UserId, in.Id).Find(&product)

	if product.UserId != in.UserId {
		return nil, fmt.Errorf("只能修改属于自己的商品")
	}

	product.Title = in.Title
	product.Price = float64(in.Price)
	product.ClassId = in.ClassId

	mysql.DB.Updates(&product)
	return &productRpc.UpdateProductResponse{
		Id: int64(product.ID),
	}, nil
}
